// MainFrm.cpp : implementation of the CMainFrame class
//
/*==============================================================================
             Copyright (c) 2009 - All rights reserved
================================================================================
File description:

    Mainframe function definition

================================================================================
      Date            Name                    Description of Change
24-Aug-2009  Xiaofeng.Gu                    Written
14-Sep-2009  Jorbin Gao                       add load Knot protect
$HISTORY$
==============================================================================*/

#include "stdafx.h"
#include "Transform.h"

#include "MainFrm.h"
#include "TransformView.h"
#include "TFTreeView.h"
#include "TimelineView.h"
#include "Global.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_COMMAND(ID_VIEW_PAN, &CMainFrame::OnViewPan)
	ON_COMMAND(ID_VIEW_ROTATE, &CMainFrame::OnViewRotate)
	ON_COMMAND(ID_SELECT_NODE, &CMainFrame::OnSelectNode)
	ON_COMMAND(ID_FILE_SAVE, &CMainFrame::OnFileSave)
	ON_COMMAND(ID_FILE_OPEN, &CMainFrame::OnFileOpen)
	ON_COMMAND(ID_TIMELINE_FLAG, &CMainFrame::OnFlag)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrame construction/destruction


CMainFrame::CMainFrame()
: m_pTimelineView(NULL),
  m_FlagCheckStatus(MF_CHECKED),
  m_PanCheckStatus(MF_CHECKED),
  m_RotateCheckStatus(MF_CHECKED)
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	CRect rect;
	GetClientRect(rect);

	// add vertical splitter
	if (!m_wndMainSplitter.CreateStatic(this, 1, 2)) return FALSE;
	// First splitter pane
	if (!m_wndMainSplitter.CreateView(0, 0,
		RUNTIME_CLASS(CTFTreeView), CSize(200,0), pContext)) return FALSE;
	CWnd* pTreePan = m_wndMainSplitter.GetPane(0, 0);
	m_pTreeView = dynamic_cast<CTFTreeView*>(pTreePan);

	// add vertical splitter
	m_wndViewSplitter.CreateStatic(&m_wndMainSplitter, 2, 1, WS_CHILD | WS_VISIBLE, m_wndMainSplitter.IdFromRowCol(0, 1));
	// Second splitter pane
	if (!m_wndViewSplitter.CreateView(0, 0,
		RUNTIME_CLASS(CTransformView), CSize(0, rect.Height() * 4 / 5), pContext)) return FALSE;
	CWnd* pOpenGLPan = m_wndViewSplitter.GetPane(0, 0);
	m_pMainView = dynamic_cast<CTransformView*>(pOpenGLPan);
	// third splitter pane
	if (!m_wndViewSplitter.CreateView(1, 0,
		RUNTIME_CLASS(CTimelineView), CSize(0,0), pContext)) return FALSE;
	CWnd* pTimelinePan = m_wndViewSplitter.GetPane(1, 0);
	m_pTimelineView = dynamic_cast<CTimelineView*>(pTimelinePan);

	m_pTimelineView->ModifyStyle(NULL, WS_VSCROLL | WS_HSCROLL);

	CDocument* pDoc = m_pMainView->GetDocument();
	m_pTargetDoc = dynamic_cast<CTransformDoc*>(pDoc);
	m_pTargetDoc->SetMainFrame(this);

	m_pTimelineView->SetDoc(m_pTargetDoc);

	return TRUE;
}

void CMainFrame::OnViewPan()
{
	if (CSKGlobal::GetViewer()->GetOperateType() == CGViewer::eRotate)
	{
		AfxMessageBox(_T("Please close rotating function firstly!"));
		return;
	}

	CMenu* pMenu = this->GetMenu();
	m_PanCheckStatus = pMenu->CheckMenuItem(ID_VIEW_PAN, m_PanCheckStatus);

	bool bPaning = false;
	m_PanCheckStatus == MF_CHECKED ? bPaning = false : bPaning = true;
	if (bPaning)
		CSKGlobal::GetViewer()->SetOperateType(CGViewer::ePan);
	else
		CSKGlobal::GetViewer()->SetOperateType(CGViewer::eNoneOperate);
}

void CMainFrame::OnViewRotate()
{
	if (CSKGlobal::GetViewer()->GetOperateType() == CGViewer::ePan)
	{
		AfxMessageBox(_T("Please close paning function firstly!"));
		return;
	}

	CMenu* pMenu = this->GetMenu();
	m_RotateCheckStatus = pMenu->CheckMenuItem(ID_VIEW_ROTATE, m_RotateCheckStatus);

	bool bRotating = false;
	m_RotateCheckStatus == MF_CHECKED ? bRotating = false : bRotating = true;
	if (bRotating)
		CSKGlobal::GetViewer()->SetOperateType(CGViewer::eRotate);
	else
		CSKGlobal::GetViewer()->SetOperateType(CGViewer::eNoneOperate);
}

void CMainFrame::OnSelectNode()
{
	CSKGlobal::GetViewer()->SetOperateType(CGViewer::ePreSelect);
}

CString CMainFrame::GetFilePath()
{
	TCHAR szExeFilePath[512] = _T("");
	GetModuleFileName(NULL,szExeFilePath, 512);

	size_t iFilePathLength = _tcslen(szExeFilePath);
	while(szExeFilePath[iFilePathLength] != '\\')
		szExeFilePath[iFilePathLength --] = '\0';

	return (CString)szExeFilePath;
}

BOOL CMainFrame::IsFileExist(CString strFileName)
{
	WIN32_FIND_DATA FindFileData;
	HANDLE hFind;

	hFind = FindFirstFile( (LPCTSTR)strFileName, &FindFileData );
	if ( hFind == INVALID_HANDLE_VALUE )
		return FALSE;

	return TRUE;
}

void CMainFrame::OnFileSave()
{
	LPCTSTR szTypes = _T("XML Files (*.xml)|*.xml|");

	CFileDialog dlg(FALSE, _T("xml"), NULL, OFN_OVERWRITEPROMPT, szTypes );
	if ( dlg.DoModal() == IDCANCEL )
		return;

	CString csFilename = dlg.GetPathName();
	m_pTargetDoc->Save(csFilename);
}


void CMainFrame::OnFileOpen()
{
	LPCTSTR szTypes = _T("XML Files (*.xml)|*.xml|");

	CFileDialog dlg(TRUE, _T("xml"), NULL, OFN_OVERWRITEPROMPT, szTypes );
	if ( dlg.DoModal() == IDCANCEL )
		return;

	CString csFilename = dlg.GetPathName();
	m_pTargetDoc->Load(csFilename);

	CSKInfo* pSKInfo = CSKGlobal::GetTargetDoc()->GetSKInfo();
	CALLPIPELINE(CSKFactory::eLoadFile, OnExecute, CSKInfo, pSKInfo, SKINFO_INSTANCE);
}

void CMainFrame::OnFlag()
{
	CMenu* pMenu = this->GetMenu();
	m_FlagCheckStatus = pMenu->CheckMenuItem(ID_TIMELINE_FLAG, m_FlagCheckStatus);

	bool bAdding = false;
	m_FlagCheckStatus == MF_CHECKED ? bAdding = false : bAdding = true;
	CSKGlobal::GetTimelineView()->SetFlagAddingStatus(bAdding);
}